package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;

import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    /**
     * 分页条件查询
     * @param queryPageBean
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public Result queryPage(QueryPageBean queryPageBean) {
        Result result;
        String queryString = queryPageBean.getQueryString();//查询条件

        Integer pageSize = queryPageBean.getPageSize();//每页显示条数

        Integer currentPage = queryPageBean.getCurrentPage();//当前页

        //使用mybatis的PageHelper进行分页处理
        PageHelper.startPage(currentPage, pageSize);


        List<CheckItem> list = checkItemDao.findByCondition(queryString);

        if (list.isEmpty()) {
            result = new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        } else {
            result = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, new PageInfo<>(list));
        }

        return result;
    }


    /**
     * 添加检查项
     * @param checkItem
     * @return
     */
    @Override
    public Result addCheckItem(CheckItem checkItem) {
        try {
            checkItemDao.addCheckItem(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }

    }


    /**
     * 更新检查项
     * @param checkItem
     * @return
     */
    @Override
    public Result updateCheckItem(CheckItem checkItem) {
        try {
            checkItemDao.updateCheckItem(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @Override
    public Result delete(Integer id) {
        //先查询该记录在中间表中有没有关联检查组
        Integer count = checkItemDao.findCountByCheckItemId(id);
        if (count > 0)
            //关联项不能删除,抛出异常上层接口捕获
          throw new RuntimeException(MessageConstant.DELETE_CHECKITEM_FAIL);


        checkItemDao.delete(id);

       return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);

    }


    /**
     * 查询所有检查项
     * @return
     */
    @Override
    public Result findAll() {
        List<CheckItem> list = checkItemDao.findAll();

        return  new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }


}
